package com.cennavi.minemapdemo.activity.exlayer;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cennavi.minemapdemo.R;
import com.cennavi.minemapdemo.adapter.IndoorAdapter;
import com.cennavi.minemapdemo.util.Config;
import com.minedata.minemap.MinemapAccountManager;
import com.minedata.minemap.camera.CameraPosition;
import com.minedata.minemap.geometry.LatLng;
import com.minedata.minemap.map.IndoorMap;
import com.minedata.minemap.map.MapView;
import com.minedata.minemap.map.MineMap;

import java.util.ArrayList;
import java.util.List;

public class IndoorMapActivity extends AppCompatActivity {
    private IndoorMap indoorMap;
    private LinearLayout mIndoorLayout;
    private RecyclerView mIndoorRecyclerView;
    private ImageView mTopArrowImageView;
    private ImageView mBottomArrowImageView;
    private IndoorAdapter mIndoorAdapter;
    private List<String> mIndoorNameList = new ArrayList<>();

    private MapView mMapView;
    private static MineMap mMineMap;
    private int mCurrentZoom = 0;
    private Context mContext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        MinemapAccountManager.getInstance(this, Config.MAP_NIGHT_TOK, Config.OFFLINE_TOK, Config.MAP_NIGHT_ID);
        setContentView(R.layout.activity_indoormap);
        initView();

        mMapView = findViewById(R.id.mapView);
        mMapView.setApiBaseUrl(Config.API_BASE_URL);
        mMapView.setApiUrl(Config.API_URL);
        mMapView.setSpriteImageUrl(Config.SPRITE_GLYPHS_URL);
        mMapView.addMapRenderCallback(new MapView.OnMapReadyListener() {
            @Override
            public void onMapReady(MineMap mineMap) {
                mMineMap = mineMap;
                mMineMap.setStyleUrl(Config.BASE_URL + Config.MAP_ID, new MineMap.OnStyleLoadedListener() {
                    @Override
                    public void onStyleLoaded(String s) {
                        initIndoorMap();

                        mMineMap.getUiSettings().setZoomControlsEnabled(false);
                        mMineMap.getUiSettings().setRotateGesturesEnabled(true);// 是否可以旋转地图
                        mMineMap.getUiSettings().setTiltGesturesEnabled(true);
                        mMineMap.getUiSettings().setCompassEnabled(true);
                        mMineMap.setMaxZoomPreference(20);
                        mMineMap.setMinZoomPreference(3);

                        mMineMap.setCameraPosition(
                                //34.2419937,108.9418989 西安
                                new CameraPosition.Builder()
                                        .target(new LatLng(41.736599, 123.464134))//设置相机指向的位置
                                        .zoom(15)//设置相机缩放等级
                                        .tilt(0)//设置相机的俯视角度
                                        .bearing(0) //摄像机指向的方向,从北部顺时针方向设置
                                        .build());

                        mMineMap.addOnScaleChangedListener(new MineMap.OnScaleChangedListener() {
                            @Override
                            public void onScaleChanged(int i) {
                                if (mCurrentZoom != i) {
                                    if (indoorMap != null) {
                                        indoorMap.onMoveMap();
                                    }
                                    mCurrentZoom = i;
                                }
                            }
                        });

                        mMineMap.addOnCameraIdleListener(new MineMap.OnCameraIdleListener() {
                            @Override
                            public void onCameraIdle() {
                                if (indoorMap != null) {
                                    indoorMap.onMoveMap();
                                }
                            }
                        });
                    }
                });
            }
        });
    }

    private void initView() {
        mIndoorLayout = findViewById(R.id.layout_indoor);
        mIndoorRecyclerView = findViewById(R.id.rv_indoor_control);
        LinearLayoutManager indoorlayoutManager = new LinearLayoutManager(this);
        mIndoorRecyclerView.setLayoutManager(indoorlayoutManager);
        mIndoorAdapter = new IndoorAdapter(mContext, mIndoorNameList);
        mIndoorAdapter.setListener(new IndoorAdapter.ISelectListener() {
            @Override
            public void onSelect(String indoorName) {
                indoorMap.setCurrentFloorName(indoorName);
            }
        });
        mIndoorRecyclerView.setAdapter(mIndoorAdapter);

        mTopArrowImageView = findViewById(R.id.iv_top_arrow);
        mBottomArrowImageView = findViewById(R.id.iv_bottom_arrow);

        mIndoorRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                mTopArrowImageView.setVisibility(View.VISIBLE);
                mBottomArrowImageView.setVisibility(View.VISIBLE);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
                if (firstCompletelyVisibleItemPosition == 0)
                    mBottomArrowImageView.setVisibility(View.INVISIBLE);

                int lastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition();
                if (lastCompletelyVisibleItemPosition == layoutManager.getItemCount() - 1)
                    mTopArrowImageView.setVisibility(View.INVISIBLE);
            }
        });
    }

    private void initIndoorMap() {
        indoorMap = new IndoorMap(mContext, mMineMap, mMapView);
        indoorMap.setIndoorDataUrl("http://211.151.84.33:10888/");

        indoorMap.addOnIndoorMapListener(new IndoorMap.OnIndoorMapListener() {

            @Override
            public void onIndoorMapView() {

            }

            @Override
            public void onIndoorMapHide() {

            }

            @Override
            public void onChangeFloor(List<String> floorList, String daultFloor) {
                if (floorList != null && floorList.size() > 0) {
                    mIndoorLayout.setVisibility(View.VISIBLE);
                    mIndoorAdapter.update(floorList, daultFloor);
                } else {
                    mIndoorLayout.setVisibility(View.GONE);
                }
            }
        });

        indoorMap.onMoveMap();
    }


    @Override
    protected void onStart() {
        if (mMapView != null) {
            mMapView.onStart();
        }
        super.onStart();
    }

    @Override
    protected void onResume() {
        if (mMapView != null)
            mMapView.onResume();
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (mMapView != null)
            mMapView.onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        if (mMapView != null)
            mMapView.onStop();
        super.onStop();
    }

    @Override
    public void onDestroy() {
        if (mMapView != null)
            mMapView.onDestroy();
        super.onDestroy();
    }
}
